package com.sxdx.service.impl;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import com.sxdx.dao.SerialcodeDAO;
import com.sxdx.entity.Serialcode;
import com.sxdx.service.SerialcodeService;

@SuppressWarnings("all")
@Service("serialcodeService")
public class SerialcodeServiceImpl implements SerialcodeService {
	private static Logger logger = LogManager.getLogger(Thread.currentThread().getStackTrace()[1].getClassName());
	@Resource(name = "serialcodeDAO")
	private SerialcodeDAO serialcodeDAO;

	public List<Serialcode> keygen(int rows, int cols) {
		serialcodeDAO.saveSerialcode(rows, cols);
		return null;
	}

	public boolean clsAllCode() {
		serialcodeDAO.clsSerialcode();
		return true;
	}

	public boolean setCodeFlag(String regcode) {
		String hql = "from Serialcode s where s.regCode= ?";
		List<Object> param = new ArrayList<Object>();
		param.add(regcode);
		List<Serialcode> l = serialcodeDAO.find(hql, param);
		if (l.size() > 0) {
			l.get(0).setCodeFlag("已使用");
			return true;
		} else {
			return false;
		}
	}

	public boolean clearCodeused() {
		return false;
	}

	public List<Serialcode> codeFind(String hql, Integer page, Integer rows) {
		List<Serialcode> codes = serialcodeDAO.page(hql, page, rows);
		return codes;
	}

	public void deleteById(String id) {
		serialcodeDAO.delete(serialcodeDAO.get(Serialcode.class, Integer.parseInt(id)));
	}

	public int findall() {
		String hql = "from Serialcode";
		return serialcodeDAO.find(hql).size();
	}
}
